package org.example;

import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@RestController
@RequestMapping("/redis")
public class RedissonClientTest {

    //static final Logger logger = LoggerFactory.getLogger(DemoApplicationTests.class);

    // 注入 RedissonClient
    @Autowired
    RedissonClient redissonClient;

    // 计数器
    private int count;


    @GetMapping("/test")
    public void test() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1000);
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                // 每个线程都创建自己的锁对象
                // 这是基于 Redis 实现的分布式锁
                Lock lock = this.redissonClient.getLock("counterLock");

                try {
                    // 上锁
                    lock.lock();

                    // 计数器自增 1
                    this.count = this.count + 1;

                } finally {
                    // 释放锁
                    lock.unlock();
                }
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        System.out.println("count = {}" + this.count);
//        logger.info("count = {}", this.count);
    }

    @GetMapping("/test2")
    public void test2() {
        System.out.println("test2..." + Thread.currentThread().getId());

//        try {
//            getThreadID();
//        } catch (UnknownHostException e) {
//            throw new RuntimeException(e);
//        }
        // 这是基于 Redis 实现的分布式锁
        Lock lock = this.redissonClient.getLock("counterLock2");
        try {
            // 上锁
            lock.lock();
            lock.lock();

            TimeUnit.SECONDS.sleep(120);

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            // 释放锁
            lock.unlock();
        }


        System.out.println("test2...end");
    }

    private void getThreadID() throws UnknownHostException {


        String id = Thread.currentThread().getId() + ":"
                + InetAddress.getLocalHost().getHostName() + ":"
                + ManagementFactory.getRuntimeMXBean().getName().split("@")[0];


        System.out.println("id = " + id);

    }


    @GetMapping("/test1")
    public void test1() {
        System.out.println("test1..." + Thread.currentThread().getId());
        // 这是基于 Redis 实现的分布式锁
        Lock lock = this.redissonClient.getLock("counterLock1");
        try {
            // 上锁
            lock.lock();

            TimeUnit.SECONDS.sleep(200);

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            // 释放锁
            lock.unlock();
        }


        System.out.println("test1...end");
    }


}
